/*
 * Copyright (c) [2012] - [2017] Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package com.codenvy.machine.agent;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.eclipse.che.commons.lang.IoUtil;

/**
 * Reads content of public key that is used for workspaces projects synchronization from file.
 *
 * @author Alexander Garagatyi
 */
@Singleton
public class WorkspaceSyncPublicKeyProvider implements Provider<String> {
  private String pubKey;

  @Inject
  public WorkspaceSyncPublicKeyProvider(
      @Named("workspace.backup.public_key_path") String pubKeyPath) throws IOException {

    pubKey =
        IoUtil.readAndCloseQuietly(
            new BufferedInputStream(new FileInputStream(new File(pubKeyPath))));
  }

  @Override
  public String get() {
    return pubKey;
  }
}
